<?php
/**
 * Created by PhpStorm.
 * User: xiemi
 * Date: 2020/4/2
 * Time: 20:25
 */

namespace darksun;


class Route
{
    public $app;
    protected $method;
    protected $url;
    protected $param;
    protected $controller;
    protected $action;

    public function __construct()
    {

    }

    public function bootService(App $app){
        $this->app = $app;
        $this->method = $this->method();
        $this->url = $this->url();
        $this->controller = $this->parseController();
        $this->action = $this->parseAction();
        $this->param = $this->parseParam();
        $controllerClass = 'app\\controller\\'.ucfirst($this->controller).'Controller';
        $controller = $this->app->make($controllerClass);
        call_user_func_array([$controller, $this->action], $this->param);
    }

    protected function method(){
        if (!$this->method) {
            if ($this->app->request->post('_method')) {
                $method = strtolower($this->app->request->post('_method'));
                if (in_array($method, ['get', 'post', 'put', 'patch', 'delete'])) {
                    $this->method = strtoupper($method);
                } else {
                    $this->method = 'POST';
                }
            } elseif ($this->app->request->server('HTTP_X_HTTP_METHOD_OVERRIDE')) {
                $this->method = strtoupper($this->app->request->server('HTTP_X_HTTP_METHOD_OVERRIDE'));
            } else {
                $this->method = $this->app->request->server('REQUEST_METHOD')?: 'GET';
            }
        }
        return $this->method;
    }

    protected function url(){
        if($this->url){
            $url = $this->url;
        }elseif($this->app->request->server('HTTP_X_REWRITE_URL')){
            $url = $this->app->request->server('HTTP_X_REWRITE_URL');
        }elseif ($this->app->request->server('REQUEST_URI')){
            $url = $this->app->request->server('REQUEST_URI');
            
        }elseif ($this->app->request->server('ORIG_PATH_INFO')){
            $url = $this->app->request->server('ORIG_PATH_INFO') . (!empty($this->app->request->server('QUERY_STRING')) ? '?' . $this->app->request->server('QUERY_STRING') : '');
        }elseif(isset($_SERVER['argv'][1])){
            $url = $_SERVER['argv'][1];
        }else{
            $url = '';
        }
        return $url;
    }

    protected function parseController(){
        $controller = 'index';
        $paths = explode('/',$this->url);
        if($paths[0] !== ''){
            $controller = $paths[0];
        }
        return $controller;
    }

    protected function parseAction(){
        $action = 'index';
        $paths = explode('/',$this->url);
        if(isset($paths[1]) && $paths[1] !== ''){
            $action = $paths[1];
        }
        return $action;
    }

    protected function parseParam()
    {
        $param = [
            'method' => $this->method,
            'app' => $this->app,
        ];
        $paths = explode('/',$this->url);
        $count = count($paths);
        if($count > 3){
            unset($paths[0]);
            unset($paths[1]);
            for($i = 0; $i < $count - 2; $i++){
                if($i % 2) $param[$paths[$i]] =  $paths[$i+1];
            }
        }
        return $param;
    }

}